# coding=utf-8
import numpy as np
import os
# items和choices初始默认是下方的情况,但也可以自己修改好放入文件中,然后自行通过loadItemsChoices进行加载
items = ['soccer' , 'basketball' , 'tennis' , \
         'swimming' , 'computer' , 'Lol' , \
         'dota' , 'shopping' , 'hiking' , \
         'running' , 'consume' , 'comic' , \
         'travel' , 'communicate']

choices = [
    ['negative' , 'indifferent' , 'positive'] , ['negative' , 'indifferent' , 'positive'] , ['negative' , 'indifferent' , 'positive'] ,
    ['negative' , 'indifferent' , 'positive'] , ['negative' , 'indifferent' , 'positive'] , ['negative' , 'indifferent' , 'positive'] ,
    ['negative' , 'indifferent' , 'positive'] , ['sometimes' , 'often' , 'usually'] , ['negative' , 'indifferent' , 'positive'] ,
    ['negative' , 'indifferent' , 'positive'] , ['low' , 'middle' , 'high'] , ['negative' , 'indifferent' , 'positive'] ,
    ['less_than4' , 'equal_4' , 'more_than_4'] , ['wechat' , 'QQ' , 'weibo']
]
# 读取社交用户特征数据
def getDataset(dir='trainData/', pre='train', tail='.txt'):
    # 判断文件目录是否存在
    if not os.path.exists(dir):
        print '你查找的文件目录'+dir+'不存在'

    dataSet = []
    index = 1
    while os.path.exists(dir+pre+str(index)+tail):
        fp = open(dir+pre+str(index)+tail)
        print fp.readline()
        features = fp.readline()
        print features
        dataSet.append(str(features).split('\t'))
        fp.close()
        index += 1
    print "训练集合一共有",index-1,"组数据"
    return dataSet


# 加载所有的可选项目和选择项,方便我们之后判断用户的选择属于什么位置,给怎么样的评价
def loadItemsChoices(file='public/items_choice.txt'):
    # 判断文件是否存在
    if not os.path.exists(file):
        print "您选择的文件路劲"+file+"不存在,请重新加载正确的文件"
        return
    fp = open(file)
    items = []
    choices = []
    print "here"
    lines = fp.readlines()
    for line in lines:
        print line
        line.replace("\n" , "")
        print line.split('\t')
        line = line[:len(line)-1]
        val = line.split('\t')
        items.append(val[0])
        choices.append(list(val[1:4]))
    print items
    print choices


# 筛选出所有和userId是好友的id然后对应位置标记为1
def getFriendVector(userId, vectorId, path='public/trainFriendship.txt'):
    # 判断文件是否存在
    if not os.path.exists(path):
        print "好友关系的文件: ",path,"不存在"
        return

    friends = []
    fp = open(path)
    lines = fp.readlines()
    fp.close()
    for line in lines:
        if line[len(line)-1] == '\n':
            line = line[:len(line)-1]
        val = line.split('\t')
        if int(val[0]) == userId:
            friends.append(int(val[1]))
        elif int(val[1]) == userId:
            friends.append(int(val[0]))
    friends.sort()
    vectorFriend = np.zeros(len(vectorId))
    for user in friends:
        try:
            pos = vectorId.index(user)
            vectorFriend[pos] = 1
        except ValueError:
            print user, "用户信息不在列表中"
    return vectorFriend